package cn.mygweb.admin.modules.sys.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import cn.mygweb.admin.modules.sys.pojo.Role;
import cn.mygweb.admin.modules.sys.pojo.RoleMenu;
import cn.mygweb.common.controller.BaseController;
import cn.mygweb.common.pojo.JsonResult;
import io.lettuce.core.dynamic.annotation.Param;

/** 
* <p>Title: RoleController.java</p>
* <p>Description: 系统角色管理</p>  
* @author gxb 
* @date 2019年6月7日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
@Controller
@RequestMapping("/admin/sys/role")
public class RoleController extends BaseController{

	/**
	 * <p>Title: role</p>
	 * <p>Description: 访问角色管理页面</p>
	 * @return
	 */
	@RequestMapping("/role")
	public String role() {
		return "admin/modules/sys/role";
	}
	
	/**
	 * <p>Title: ajaxRolesList</p>
	 * <p>Description: 异步获取角色信息</p>
	 * @return
	 */
	@RequestMapping("/ajaxRolesList")
	@ResponseBody
	public JsonResult ajaxRolesList(String userId, boolean available) {
		List<Role> roleList = new ArrayList<Role>();
		if(userId == null || StringUtils.isEmptyOrWhitespace(userId)) {
			roleList = roleService.queryRoleList(new Role(),available);
		}else {
			roleList = roleService.queryRoleListByUserId(userId,available);
		}
		if(roleList != null) {
			return JsonResult.ok(roleList);
		}else {
			return JsonResult.ok("暂无角色信息");
		}
	}
	
	/**
	 * <p>Title: operation</p>
	 * <p>Description: 角色的操作，根据前台zTree返回的oper属性区别是增、删、改中的操作类型</p>
	 * @param role
	 * @param oper
	 * @return
	 */
	@RequestMapping("/operation")
	@ResponseBody
	public JsonResult operation(Role role, String oper) {
		this.delAllRedisCache();
		if(oper.equals("add")) {
			try {
				role.setId(sid.nextShort());
				roleService.add(role);
				return JsonResult.ok("添加成功");
			} catch (Exception e) {
				e.printStackTrace();
				return JsonResult.errorMsg("添加失败");
			}
		}else if(oper.equals("edit")) {
			try {
				roleService.update(role);
				return JsonResult.ok("修改成功");
			} catch (Exception e) {
				e.printStackTrace();
				return JsonResult.errorMsg("修改失败");
			}
		}else if(oper.equals("del")) {
			try {
				String ids = role.getId();
				String[] idArr = ids.split(",");
				roleService.delete(idArr);//批量删除
				return JsonResult.ok("删除成功");
			} catch (Exception e) {
				e.printStackTrace();
				return JsonResult.errorMsg("删除失败");
			}
		}
		return null;
	}
	
	/**
	 * <p>Title: setRoleMenusAuth</p>
	 * <p>Description: 设置用户的权限信息</p>
	 * @return
	 */
	@RequestMapping("/setRoleMenusAuth")
	@ResponseBody
	public JsonResult setRoleMenusAuth(@Param(value="roleId")String roleId, @Param(value="menuIds")String menuIds) {
		try {
			//1.批量删除sys_role_menu表中角色id为roleId的对应菜单信息
			roleMenuService.batchDeleteByRId(roleId);
			//2.批量添加角色的菜单信息
			List<RoleMenu> rmList = new ArrayList<RoleMenu>();
			if(menuIds != null && !StringUtils.isEmptyOrWhitespace(menuIds)) {
				String[] idArr = menuIds.split(",");
				for (String mid : idArr) {
					if(!StringUtils.isEmptyOrWhitespace(mid)) {
						RoleMenu rm = new RoleMenu();
						rm.setRid(roleId);
						rm.setId(sid.nextShort());
						rm.setMid(mid);
						rmList.add(rm);
					}
				}
				roleMenuService.insertRMList(rmList);
			}
			this.delAllRedisCache();
			return JsonResult.ok("设置成功");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.errorMsg("操作异常");
		}
	}
	
}
